// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "name": {
    "rules": [
      {
        "required": true,
        "errorMessage": "用户姓名不能为空"
      },
      {
        "format": "string"
      },
      {
        "minLength": 1,
        "maxLength": 50,
        "errorMessage": "用户姓名不能为空"
      }
    ],
    "title": "用户姓名",
    "label": "用户姓名"
  },
  "contact": {
    "rules": [
      {
        "required": true,
        "errorMessage": "联系方式不能为空"
      },
      {
        "format": "string"
      },
      {
        "minLength": 5,
        "maxLength": 100,
        "errorMessage": "联系方式格式不正确"
      }
    ],
    "title": "联系方式",
    "label": "联系方式"
  },
  "type": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "功能建议",
            "text": "功能建议"
          },
          {
            "value": "问题反馈",
            "text": "问题反馈"
          },
          {
            "value": "使用体验",
            "text": "使用体验"
          },
          {
            "value": "其他",
            "text": "其他"
          }
        ],
        "errorMessage": "反馈类型只能是：功能建议、问题反馈、使用体验、其他"
      }
    ],
    "title": "反馈类型",
    "label": "反馈类型"
  },
  "description": {
    "rules": [
      {
        "required": true,
        "errorMessage": "反馈描述不能为空"
      },
      {
        "format": "string"
      },
      {
        "minLength": 5,
        "maxLength": 1000,
        "errorMessage": "反馈描述至少5个字符"
      }
    ],
    "title": "详细描述",
    "label": "详细描述"
  },
  "images": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "图片附件",
    "label": "图片附件"
  },
  "clientIP": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "客户端IP",
    "label": "客户端IP"
  },
  "userAgent": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "用户代理",
    "label": "用户代理"
  },
  "source": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "来源",
    "label": "来源"
  },
  "createTime": {
    "rules": [
      {
        "format": "number"
      }
    ],
    "title": "创建时间戳",
    "label": "创建时间戳"
  },
  "createDate": {
    "rules": [
      {
        "format": "date"
      }
    ],
    "title": "创建日期",
    "label": "创建日期"
  },
  "status": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "pending",
            "text": "pending"
          },
          {
            "value": "processing",
            "text": "processing"
          },
          {
            "value": "resolved",
            "text": "resolved"
          },
          {
            "value": "closed",
            "text": "closed"
          }
        ],
        "errorMessage": "状态只能是：pending（待处理）、processing（处理中）、resolved（已解决）、closed（已关闭）"
      }
    ],
    "title": "处理状态",
    "label": "处理状态"
  },
  "priority": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "value": "low",
            "text": "low"
          },
          {
            "value": "normal",
            "text": "normal"
          },
          {
            "value": "high",
            "text": "high"
          },
          {
            "value": "urgent",
            "text": "urgent"
          }
        ],
        "errorMessage": "优先级只能是：low（低）、normal（普通）、high（高）、urgent（紧急）"
      }
    ],
    "title": "优先级",
    "label": "优先级"
  },
  "adminReply": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 2000,
        "errorMessage": "回复内容不能超过2000个字符"
      }
    ],
    "title": "管理员回复",
    "label": "管理员回复"
  },
  "replyTime": {
    "rules": [
      {
        "format": "date"
      }
    ],
    "title": "回复时间",
    "label": "回复时间"
  },
  "tags": {
    "rules": [
      {
        "format": "array"
      }
    ],
    "title": "标签",
    "label": "标签"
  },
  "isDeleted": {
    "rules": [
      {
        "format": "bool"
      }
    ],
    "title": "是否删除",
    "label": "是否删除"
  },
  "version": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "版本号",
    "label": "版本号"
  }
}

const enumConverter = {
  "type_valuetotext": {
    "功能建议": "功能建议",
    "问题反馈": "问题反馈",
    "使用体验": "使用体验",
    "其他": "其他"
  },
  "status_valuetotext": {
    "pending": "pending",
    "processing": "processing",
    "resolved": "resolved",
    "closed": "closed"
  },
  "priority_valuetotext": {
    "low": "low",
    "normal": "normal",
    "high": "high",
    "urgent": "urgent"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
